package com.qf.controller;

import com.qf.common.R;
import com.qf.constatns.RabbitMQConstatns;
import com.qf.feign.api.GoodsService;
import com.qf.vo.Goods;
import com.qf.vo.Stock;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/goodsController")
@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/getGoodsList")
    public String getGoodsList(ModelMap modelMap) {
        List<Goods> list = goodsService.list();
        modelMap.put("goodsList", list);
        return "goods/goodsList";
    }

    /**
     * 这里是哟一个分布式事务的问题
     *
     * @param goods
     * @return
     */
    @PostMapping("/addGoods")
    @ResponseBody
    public R addGoods(Goods goods) {

        // 1.添加商品信息
        System.out.println("goods之前:" + goods);
        goods = goodsService.saveGoodsAndGstock(goods); // 把user转成JSON传递到user服务中
        System.out.println("goods之后:" + goods);

        // 3.把goods数据发送到MQ
        rabbitTemplate.convertAndSend(RabbitMQConstatns.GOODS_EXCHAGNE, "", goods);
        return R.success();
    }

}
